﻿/**
 * Author:
 *				Đỗ Việt Hoà
 * Copyright (c) 2012 Đỗ Việt Hoà <vit@launchar.com>.
 * 
 * This file is part of Launchar.
 * 
 * Launchar is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Launchar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Launchar.  If not, see <http://www.gnu.org/licenses/>.
 */

using System.Collections;
using System.Collections.Generic;
using System.Xml;

namespace vIT.Web.Configuration
{
	/// <summary>
	/// Configuration of multi-provider.
	/// </summary>
	public class ProviderConfigCollection : IEnumerable<ProviderConfig>
	{
		private Dictionary<string, ProviderConfig> __data = null;

		#region Properties

		/// <summary>
		/// Gets the <see cref="vIT.Web.Configuration.ProviderConfig"/> object that contains the provider configuration with the provider name.
		/// </summary>
		/// <param name="name">The provider name.</param>
		public ProviderConfig this[string name] { get { return __data[name]; } }

		#endregion

		#region Constructor

		/// <summary>
		/// Initializes a new instance of <see cref="vIT.Web.Configuration.ProviderConfigCollection"/> class.
		/// </summary>
		public ProviderConfigCollection(XmlNode xml)
		{
			__data = new Dictionary<string, ProviderConfig>();
			if (xml == null) return;

			//
			foreach (XmlNode node in xml.ChildNodes)
			{
				switch (node.Name)
				{
					case "add":
						if (node.Attributes["name"] == null || node.Attributes["type"] == null) break;
						var cfg = new ProviderConfig(node);
						__data.Add(cfg.ProviderName, cfg);
						break;

					case "remove":
						if (node.Attributes["name"] == null) break;
						__data.Remove(node.Attributes["name"].Value);
						break;

					case "clear":
						__data.Clear();
						break;
				}
			}
		}

		#endregion

		#region IEnumerable<ProviderConfig> Members

		public IEnumerator<ProviderConfig> GetEnumerator()
		{ return __data.Values.GetEnumerator(); }

		#endregion

		#region IEnumerable Members

		IEnumerator IEnumerable.GetEnumerator()
		{ return __data.Values.GetEnumerator(); }

		#endregion
	}
}
